﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class ZGCSDao
    {


        public ZGCSDao()
        {

            Mapper.CreateMap<Y_ZGCS, ZGCSModel>();
        }
        public List<Y_ZGCS> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_ZGCS.Count();

                return edm.Y_ZGCS.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public ZGCSModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_ZGCS.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_ZGCS, ZGCSModel>(query);
            }

        }


        public int Save(ZGCSModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_ZGCS.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_ZGCS();

                    edm.AddToY_ZGCS(query);

                }


                query.ProjectId = obj.ProjectId;

                query.gcjs = obj.gcjs;

                query.gcsf = obj.gcsf;

                query.gcys = obj.gcys;

                query.jggl = obj.jggl;

                query.createuserid = obj.createuserid;

                query.createusername = obj.createusername;
                query.createrealname = obj.createrealname;
                query.createtime = obj.createtime;

                edm.SaveChanges();

                return query.Id;


            }

        }
        public ZGCSModel GetOneByProjectId(int id, int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_ZGCS.Where(s => s.ProjectId == id&&s.Id==reid);
                


                return Mapper.Map<Y_ZGCS, ZGCSModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }

        public ZGCSModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_ZGCS.Where(s => s.ProjectId == id);

                return Mapper.Map<Y_ZGCS, ZGCSModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }

        public List<ZGCSModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_ZGCS.Where(s => s.ProjectId == id);



                return Mapper.Map<List<Y_ZGCS>, List<ZGCSModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }

        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_ZGCS.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

